﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace FoundationLib.Web.Mvc
{
	public static class ImageTextButtonHelper
	{
		public static MvcHtmlString ImageTextButton(this AjaxHelper helper, string actionName, string controllerName,
			ImageTextButtonOptions options,	object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
		{
			string textLink = helper.ActionLink(options.Text, actionName, controllerName, routeValues, ajaxOptions, htmlAttributes)
				.ToHtmlString();
			string imageLink = "";
			string link = "";

			if (options.ShowImage)
			{
				TagBuilder builder = new TagBuilder("img");
				builder.MergeAttribute("src", UrlHelper.GenerateContentUrl(options.ImageName, helper.ViewContext.HttpContext));
				builder.MergeAttribute("alt", options.Text);
				builder.MergeAttribute("title", options.Text);
				builder.MergeAttribute("style", "vertical-align: middle;");
				if (htmlAttributes != null)
					builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

				imageLink = helper.ActionLink("{replace}", actionName, controllerName, routeValues, ajaxOptions).ToHtmlString()
					.Replace("{replace}", builder.ToString(TagRenderMode.SelfClosing));

				if (options.ImageOrientation == ImageOrientation.Right)
				{
					link = textLink + " " + imageLink;
				}
				else
				{
					link = imageLink + " " + textLink;
				}
			}
			else
			{
				link = textLink;
			}

			return MvcHtmlString.Create(link);
		}

		public static MvcHtmlString ImageTextButton(this AjaxHelper helper, string actionName, string controllerName,
			ImageTextButtonOptions options, object routeValues, AjaxOptions ajaxOptions)
		{
			return ImageTextButton(helper, actionName, controllerName, options, routeValues, ajaxOptions, null);
		}

		public static MvcHtmlString ImageTextButton(this HtmlHelper helper, string actionName, string controllerName,
			ImageTextButtonOptions options,	object routeValues, object htmlAttributes)
		{
			string textLink = helper.ActionLink(options.Text, actionName, controllerName, routeValues, htmlAttributes)
				.ToHtmlString();
			string imageLink = "";
			string link = "";

			if (options.ShowImage)
			{
				TagBuilder builder = new TagBuilder("img");
				builder.MergeAttribute("src", UrlHelper.GenerateContentUrl(options.ImageName, helper.ViewContext.HttpContext));
				builder.MergeAttribute("alt", options.Text);
				builder.MergeAttribute("title", options.Text);
				builder.MergeAttribute("style", "vertical-align: middle;");
				if (htmlAttributes != null)
					builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

				imageLink = helper.ActionLink("{replace}", actionName, controllerName, routeValues).ToHtmlString()
					.Replace("{replace}", builder.ToString(TagRenderMode.SelfClosing));

				if (options.ImageOrientation == ImageOrientation.Left)
				{
					link = imageLink + " " + textLink;
				}
				else
				{
					link = textLink + " " + imageLink;
				}
			}
			else
			{
				link = textLink;
			}

			return MvcHtmlString.Create(link);
		}

		public static MvcHtmlString ImageTextButton(this HtmlHelper helper, string actionName, string controllerName,
			ImageTextButtonOptions options, object routeValues)
		{
			return ImageTextButton(helper, actionName, controllerName, options, routeValues, null);
		}
	}
}
